import wepy from 'wepy'
import api from '@/api/api'
import {imgurl} from '@/utils/constant'
import {WXREGISTER} from '@/utils/constant'
import { Promise } from 'core-js';
export default class testMixin extends wepy.mixin {
  data = {
    mixin: 'This is mixin data.',
    relevant_showstate:false,
    imgurl:imgurl,
    list:[],
    liveList:[],//直播列表
    audioMenuData:[],//音频栏目列表
  }

  methods = {
    tap () {
      this.mixin = 'mixin data was changed'
    },

  }

  onShow() {
    
  }

  async onLoad() { 
    
  }

  //浏览量更新
  async updateArticleHits(articleid) {
      await api.UpdateArticleHits({
        query:{
          MemberId:wepy.getStorageSync(WXREGISTER).id, 
          ArticleId:articleid
        },
        }).then((res) => {
          if(res.status == 'success')
          {
            
          }
          else 
          {
            
            wepy.showToast({
              title: res.msg, 
              icon: 'none'
            });
          }
        }).catch((err) => {
          
          wepy.showToast({ 
            title: err.msg, 
            icon: 'none'
          });
      })
  };

  //图文视评论点赞
  async updateArticleCommendLiked(commendid) {
    let state;
    await api.UpdateArticleCommendLiked({
      query:{
        MemberId:wepy.getStorageSync(WXREGISTER).id,
        CommendId:commendid
      }
      }).then((res) => {
        if(res.status == 'success')
        { 
          if(res.data.action_status)
          {
            wepy.showToast({
              title: '已点赞', 
              icon: 'success'
            });
            state = true
            this.$apply();
          }
          else
          {
            wepy.showToast({
              title: '已取消', 
              icon: 'success'
            });
            state = false;
            this.$apply();
          }
        }
        else
        {
          
          wepy.showToast({
            title: res.msg, 
            icon: 'none'
          });
        }
      }).catch((err) => {
        
        wepy.showToast({ 
          title: err.msg,  
          icon: 'none'
        });
    })
    return state;
  };

  //图文视用户收藏
  async updateArticleCollection(articleid) {
    let state;
    await api.UpdateArticleCollection({ 
      query:{
        MemberId:wepy.getStorageSync(WXREGISTER).id,
        ArticleId:articleid
      }
      }).then((res) => {
        if(res.status == 'success')
        {
          if(res.data.action_status)
          {
            wepy.showToast({
              title: '已收藏！', 
              icon: 'success'
            });
            this.$apply();
            state = true;
          }
          else
          {
            wepy.showToast({
              title: '已取消！', 
              icon: 'success'
            });
            this.$apply();
            state = false;
          }
        }
        else
        {
          
          wepy.showToast({
            title: res.msg, 
            icon: 'none'
          });
        }
      }).catch((err) => {
        
        wepy.showToast({ 
          title: err.msg, 
          icon: 'none'
        });
    })

    return state;
  };

  shareFun() {
    wepy.showShareMenu({ withShareTicket: true });
  }

  //长按识别二维码
  previewImage(e)
  {
    wepy.previewImage({
      current:e.currentTarget.dataset.src,
      urls: e.currentTarget.dataset.src.split(',') //需要预览的图片链接列表,
    });    
  }

  //专家列表
  async getExpertList(ID,pagesize,currentpage,getmore,istoday) {
    let GetMore = getmore || false;
    this.$broadcast('loadingShow');
    let result;
    await api.ExpertList({
      query:{
        pageSize:pagesize || 10,
        currentPage:currentpage || 1,
        departmentID:ID,
        isOnline:false, 
        isAppointment:false,
        isQuestion:false,
        sort:true,
        isToday:istoday || 0
      }
    }).then((res) => { 
      result = res;
    }).catch((err) => {
      
      wepy.showToast({
        title: '医生数据获取失败，请稍后重试！', 
        icon: 'none'
      });
    })

    if(result.status == 'success')
    {
      if(GetMore) 
      {
        for(let i = 0; i < result.data.length; i++)
        { 
          this.list.push(result.data[i])
        }
        this.$broadcast('doctorDataFun',await this.list)
      }
      else
      {
        this.$broadcast('doctorDataFun',await result.data)
      }
      if(result.data == '')
      {
        this.moreIsShow = false;
        this.$apply();
      } 
      else
      {
          this.moreIsShow = true;
          this.$apply();
      }
    }
    else
    {
      wepy.showToast({
        title: result.msg, 
        icon: 'none'
      });
    }
    this.$broadcast('loadingHide') 
  }

  //今日在线专家列表
  async getTodayExpertList(ID,pagesize,currentpage,getmore,istoday) {
    let GetMore = getmore || false;
    this.$broadcast('loadingShow');
    let result;
    await api.ExpertList({
      query:{
        pageSize:pagesize || 10,
        currentPage:currentpage || 1,
        departmentID:ID,
        isOnline:false, 
        isAppointment:false,
        isQuestion:false,
        sort:true,
        isToday:istoday || 0
      }
    }).then((res) => { 
      result = res;
    }).catch((err) => {
      
      wepy.showToast({
        title: '医生数据获取失败，请稍后重试！', 
        icon: 'none'
      });
    })

    if(result.status == 'success')
    {
      if(GetMore)
      {
        for(let i = 0; i < result.data.length; i++)
        { 
          this.list.push(result.data[i])
        }
        this.$broadcast('todayDoctorDataFun',await this.list)
      }
      else
      {
        this.$broadcast('todayDoctorDataFun',await result.data)
      }
      if(result.data == '')
      {
        this.moreIsShow = false;
        this.$apply();
      } 
      else
      {
          this.moreIsShow = true;
          this.$apply();
      }
    }
    else
    {
      wepy.showToast({
        title: result.msg, 
        icon: 'none'
      });
    }
    this.$broadcast('loadingHide') 
  }

  //热门咨询/咨询列表
  async consultationList(ID,pagesize,currentpage,getmore,AuthorId,isnullgetall)
  {
    let requestData;
    let GetMore = getmore || false;
    let queryObj = {};

    queryObj.categoryId = ID == undefined ? 0 : ID;
    queryObj.authorId = AuthorId == undefined ? 0 : AuthorId;
    queryObj.isNullGetAll = isnullgetall == undefined ? false : isnullgetall;
    
    queryObj.pageSize = pagesize || 10;
    queryObj.currentPage = currentpage || 1;
    queryObj.memberId = wepy.getStorageSync(WXREGISTER).id;
    queryObj.sort = true;
    queryObj.sortBy = 5;
    queryObj.categoryType = 4
    await api.ArticleList({ 
      query:queryObj
      }).then((res) => {
        if(res.status == 'success')
        {
          if(GetMore)
          {
            for(let i = 0; i < res.data.length; i++)
            { 
              this.list.push(res.data[i])
            }
            this.$broadcast('consultationDataFun',this.list)
          }
          else
          {
            this.$broadcast('consultationDataFun',res.data)
          }
          requestData = res.data;
          this.$apply();
        }
        else
        {
          wepy.showToast({
            title: res.msg,
            icon: 'none'
          });
          
        } 
      }).catch((err) => {
        wepy.showToast({
          title: "咨询数据获取失败，请稍后重试！", 
          icon: 'none'
        });
      })
      return requestData;
  }
 
  //视频推荐/视频列表
  async getVideoList(ID,pagesize,currentpage,getmore,AuthorId,isnullgetall)
  {
    let requestData;
    let GetMore = getmore || false;
    let queryObj = {};

    queryObj.categoryId = ID == undefined ? 0 : ID;
    queryObj.authorId = AuthorId == undefined ? 0 : AuthorId;
    queryObj.isNullGetAll = isnullgetall == undefined ? false : isnullgetall;

    queryObj.pageSize = pagesize;
    queryObj.currentPage = currentpage || 1;
    queryObj.memberId = wepy.getStorageSync(WXREGISTER).id;
    queryObj.sort = true;
    queryObj.sortBy = 5;
    queryObj.categoryType = 3;
      await api.ArticleList({
        query:queryObj
      }).then((res) => { 
        //this.videodata = res.data;
        if(GetMore)
        {
          for(let i = 0; i < res.data.length; i++)
          { 
            this.list.push(res.data[i])
          }
          this.$broadcast('videocolumnDataFun',this.list)
        }
        else
        {
          this.$broadcast('videocolumnDataFun',res.data)
        }
        requestData = res.data;
        this.$apply();
      }).catch((err) => {
          wepy.showToast({
            title:"视频数据获取失败，请稍后重试！",  
            icon: 'none'
          }); 
      })
      return requestData;
  };

  //知识推荐/图文列表
  async getImgTextList(ID,pagesize,currentpage,getmore,AuthorId,isnullgetall) {
    let GetMore = getmore || false;
    this.$broadcast('loadingShow') 
    let requestData;
    let queryObj = {};

    queryObj.categoryId = ID == undefined ? 0 : ID;
    queryObj.authorId = AuthorId == undefined ? 0 : AuthorId;
    queryObj.isNullGetAll = isnullgetall == undefined ? false : isnullgetall;
    
    queryObj.pageSize = pagesize;
    queryObj.currentPage = currentpage || 1;
    queryObj.memberId = wepy.getStorageSync(WXREGISTER).id;
    queryObj.sort = true;
    queryObj.sortBy = 5;
    queryObj.categoryType = 2;
      await api.ArticleList({
        query:queryObj
      }).then((res) => {
       // this.videodata = res.data;
        if(GetMore)
        {
          for(let i = 0; i < res.data.length; i++)
          { 
            this.list.push(res.data[i])
          }
          this.$broadcast('informationDataFun',this.list)
        }
        else
        {
          this.$broadcast('informationDataFun',res.data)
        } 
        requestData = res.data;
        this.$apply();
      }).catch((err) => {
          wepy.showToast({
            title: "图文数据获取失败，请稍后重试！", 
            icon: 'none'
          });
      })
      this.$broadcast('loadingHide') 

      return requestData;
  }

  //直播列表
  async getLiveList()
  {
    await api.GetLiveChannels({
      method:"GET"
    }).then((res) => {
      if(res.status == "success")
      {
        this.liveList = res.data;
        this.liveList.forEach(item => {
          this.GetCaptureImg(item.channelId)
        });
        this.$apply();
      }
      else
      {
        wepy.showToast({
          title: res.msg,
          icon: 'none', 
          duration: 2000
        });
      }
    }).catch((err) => {
      wepy.showModal({
        title: '提示', //提示的标题,
        content: '列表获取失败！', //提示的内容,
        showCancel: false, //是否显示取消按钮,
        confirmText: '确定', //确定按钮的文字，默认为取消，最多 4 个字符,
        confirmColor: '#3CC51F', //确定按钮的文字颜色,
        success: res => {
            if(res.confirm)
            {
                wepy.navigateBack({
                delta:1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
                });
            }
        }
        });
    })
  }

    //获取直播截图
    async GetCaptureImg(channelId)
    {
      await api.GetCaptureImg({
        query:{
          channelId:channelId
        },
        method:"GET"
      }).then((res) => {
        
        if(res.status == 'success')
        {
          this.liveList.forEach((item,index) => {
            if(item.channelId == channelId)
            {
              this.liveList[index].imgs = res.data.data;
              this.liveList[index].state = res.data.code;
            }
          });
          this.$apply();
        }
        else
        {
          wepy.showModal({
            title: '提示', //提示的标题,
            content: res.msg, //提示的内容,
            showCancel: false, //是否显示取消按钮,
            confirmText: '确定', //确定按钮的文字，默认为取消，最多 4 个字符,
            confirmColor: '#3CC51F', //确定按钮的文字颜色,
            success: res => {
              if(res.confirm)
              {
                  wepy.navigateBack({
                    delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
                  });
              }
            }
          });
        }
      }).catch((err) => {
        wepy.showModal({
          title: '提示', //提示的标题,
          content: '直播截图获取失败！', //提示的内容,
          showCancel: false, //是否显示取消按钮,
          confirmText: '确定', //确定按钮的文字，默认为取消，最多 4 个字符,
          confirmColor: '#3CC51F', //确定按钮的文字颜色,
          success: res => {
            if(res.confirm)
            {
              wepy.navigateBack({
                delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
              });
            }
          }
        });
      })
    }

    //音频推荐栏目
    async getAudioMenu() {
      await api.CategoryList({
        query:{
            pageSize:10, 
            currentPage:1,
            categoryId:0,
            sort:false,
            categoryType:5,
            parentID:0
        }
      }).then((res) => {
        if(res.status == "success")
        {
          this.audioMenuData = res.data;
          this.$apply();
        }
        else
        {
          wepy.showToast({
            title: res.msg, //提示的内容,
            icon: 'none', //图标,
            duration: 2000, //延迟时间,
            mask: true
          })
        }
      }).catch((err) => {
        wepy.showModal({
          title: '提示', //提示的标题,
          content: '音频栏目获取失败，请重试！', //提示的内容,
          showCancel: false, //是否显示取消按钮,
          confirmText: '确定', //确定按钮的文字，默认为取消，最多 4 个字符,
          confirmColor: '#3CC51F', //确定按钮的文字颜色,
          success: res => {
            if(res.confirm)
            {
              wepy.redirectTo({ url: 'xi' });
            }
          }
        });
      })
    }
}
